//
//  HNSH_IntegralBillManager.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/1.
//

#import "HNSH_IntegralBillManager.h"

//view
#import "JXPagerView.h"
#import "JXCategoryTitleView.h"
#import "JXCategoryIndicatorLineView.h"

#import "HNSH_IntegralBillManagerHeader.h"
#import "HNSH_HYZIntegralBillHeader.h"
#import "HNSH_LSNLIntegralBillHeader.h"

#import "HNSH_IntegralBillViewController.h"

static const CGFloat JXTableHeaderViewHeight = 154;
static const CGFloat JXheightForHeaderInSection = 44;

@interface HNSH_IntegralBillManager ()<JXPagerViewDelegate, JXPagerMainTableViewGestureDelegate, JXCategoryViewDelegate>
/**  */
@property (nonatomic, strong) JXPagerView * pagerView;
/**  */
@property (nonatomic, strong) JXCategoryTitleView * categoryView;
/**  */
@property (nonatomic, strong) HNSH_IntegralBillManagerHeader * headerView;
/**  */
@property (nonatomic, strong) HNSH_HYZIntegralBillHeader * hyzHeaderView;
/**  */
@property (nonatomic, strong) HNSH_LSNLIntegralBillHeader * lszzHeaderView;

@end

@implementation HNSH_IntegralBillManager

//MARK: Cycle Methods
- (void)viewDidLoad{
    [super viewDidLoad];
        
    if (self.zjType == 1) {
        [self showNavTitle:@"绿色能量" showLeftItem:YES];
    } else if (self.zjType == 7) {
        [self showNavTitle:@"购物值" showLeftItem:YES];
    } else if (self.zjType == 6) {
        [self showNavTitle:@"贡献值" showLeftItem:YES];
    } else if (self.zjType == 4) {
        [self showNavTitle:@"活跃值" showLeftItem:YES];
    }
    
    [self addCategoryView];
    
    [self loadNewData];
    
    [self addAllViews];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (_pagerView) {
        [_pagerView reloadData];
    }
}

- (void)addAllViews{
    [self.view addSubview:self.pagerView];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];

    self.pagerView.frame = CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight);
}

//MARK: 获取所有代币
- (void)addCategoryView{
//    if (self.zjType == 4) {
//        self.categoryView.titles = @[@"活跃度明细"];
//    } else {
        self.categoryView.titles = @[@"全部", @"消耗", @"收入"];
//    }
    [self.categoryView reloadDataWithoutListContainer];
    [self.pagerView reloadData];
//    [self.view clipCornerWithView:self.categoryView cornerWidth:12 cornerType:UIRectCornerTopLeft|UIRectCornerTopRight];
}

- (void)loadNewData{
    [HNSH_UserNetServer requestBillStatisticsWithType:[NSString stringWithFormat:@"%ld",self.zjType] success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            if (self.zjType == 1 || self.zjType == 1) {
                [self.lszzHeaderView configZjType:self.zjType info:responseObject.resultset];
            } else if (self.zjType == 4 || self.zjType == 3 || self.zjType == 5) {
                [self.hyzHeaderView configZjType:self.zjType info:responseObject.resultset];
            } else {
                [self.headerView configZjType:self.zjType info:responseObject.resultset];
            }
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

//MARK: Getter Methods
- (JXPagerView *)pagerView{
    if (!_pagerView) {
        _pagerView = [[JXPagerView alloc] initWithDelegate:self];
        _pagerView.mainTableView.gestureDelegate = self;
        _pagerView.mainTableView.backgroundColor = [UIColor clearColor];
        _pagerView.backgroundColor = [UIColor clearColor];
    }
    return _pagerView;
}

- (HNSH_IntegralBillManagerHeader *)headerView{
    if (!_headerView) {
        _headerView = [[HNSH_IntegralBillManagerHeader alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, JXTableHeaderViewHeight)];
        _headerView.zjType = self.zjType;
    }
    return _headerView;
}

- (HNSH_HYZIntegralBillHeader *)hyzHeaderView {
    if (!_hyzHeaderView) {
        _hyzHeaderView = [[HNSH_HYZIntegralBillHeader alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, JXTableHeaderViewHeight)];
        _hyzHeaderView.zjType = self.zjType;
        WeakSelf(weakSelf);
        _hyzHeaderView.billBlock = ^(NSInteger index) {
            NSLog(@"index ======> %ld",index);
            if (index == 0) {
                //团队活跃度
                weakSelf.zjType = 4;
            } else if (index == 1) {
                //加成活跃度
                weakSelf.zjType = 3;
            } else {
                //个人活跃度
                weakSelf.zjType = 5;
            }
            [weakSelf loadNewData];
            [weakSelf.pagerView reloadData];
        };
    }
    return _hyzHeaderView;
}

- (HNSH_LSNLIntegralBillHeader *)lszzHeaderView {
    if (!_lszzHeaderView) {
        _lszzHeaderView = [[HNSH_LSNLIntegralBillHeader alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, JXTableHeaderViewHeight)];
        _lszzHeaderView.zjType = self.zjType;
        WeakSelf(weakSelf);
        _lszzHeaderView.billBlock = ^(NSInteger index) {
            NSLog(@"index ======> %ld",index);
            if (index == 0) {
                //绿色能量
                weakSelf.zjType = 1;
            } else {
                //冻结能量
                weakSelf.zjType = 2;
            }
            [weakSelf loadNewData];
            [weakSelf.pagerView reloadData];
        };
    }
    return _lszzHeaderView;
}

- (JXCategoryTitleView *)categoryView{
    if (!_categoryView) {
        _categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, JXheightForHeaderInSection)];
        _categoryView.backgroundColor = [UIColor whiteColor];
        _categoryView.delegate = self;
        _categoryView.titleSelectedColor = [UIColor appBlackLabelColor];
        _categoryView.titleColor = [UIColor colorWithHexString:@"#999999"];
        _categoryView.titleFont = [UIFont boldSystemFontOfSize:16];
        _categoryView.titleSelectedFont = [UIFont boldSystemFontOfSize:16];
        _categoryView.titleColorGradientEnabled = YES;
        _categoryView.titleLabelZoomEnabled = YES;
        _categoryView.contentScrollViewClickTransitionAnimationEnabled = NO;
        _categoryView.listContainer = (id<JXCategoryViewListContainer>)self.pagerView.listContainerView;

        JXCategoryIndicatorLineView * lineView = [JXCategoryIndicatorLineView new];
        lineView.indicatorColor = [UIColor appGreenColor];
        lineView.indicatorWidth = 32;
        _categoryView.indicators = @[lineView];
    }
    return _categoryView;
}

//MARK: JXPagerViewDelegate Methods
- (UIView *)tableHeaderViewInPagerView:(JXPagerView *)pagerView{
    if (self.zjType == 1 || self.zjType == 2) {
        return self.lszzHeaderView;
    } else if (self.zjType == 4 || self.zjType == 3 || self.zjType == 5) {
        return self.hyzHeaderView;
    }
    return self.headerView;
}

- (NSUInteger)tableHeaderViewHeightInPagerView:(JXPagerView *)pagerView{
    return JXTableHeaderViewHeight;
}

- (NSUInteger)heightForPinSectionHeaderInPagerView:(JXPagerView *)pagerView{
    return JXheightForHeaderInSection;
}

- (UIView *)viewForPinSectionHeaderInPagerView:(JXPagerView *)pagerView{
    return self.categoryView;
}

- (NSInteger)numberOfListsInPagerView:(JXPagerView *)pagerView{
    //和categoryView的item数量一致
    return self.categoryView.titles.count;
}

- (id<JXPagerViewListViewDelegate>)pagerView:(JXPagerView *)pagerView initListAtIndex:(NSInteger)index{
    HNSH_IntegralBillViewController * list = [HNSH_IntegralBillViewController new];
    list.type = index;
    [list configViewWithType:index zjType:self.zjType capital_list:self.capital_list];
    WeakSelf(weakSelf);
    list.typeBlock = ^(NSInteger type) {
        weakSelf.zjType = type;
        [weakSelf loadNewData];
    };
    return list;
}

//MARK: JXCategoryViewDelegate Methods
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index{
    
}

//MARK: JXPagerMainTableViewGestureDelegate Methods
- (BOOL)mainTableViewGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    //禁止categoryView左右滑动的时候，上下和左右都可以滚动
    if (otherGestureRecognizer == self.categoryView.collectionView.panGestureRecognizer) {
        return NO;
    }
    return [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}


@end
